package net.xujialiang.XTestRPA.Service;

import net.xujialiang.XTestRPA.Entity.Tenant.XRPATenant;
import net.xujialiang.XTestRPA.Enum.TenantOrderEnum;
import net.xujialiang.XTestRPA.Repository.Tenant.TenantRepository;
import net.xujialiang.XTestRPA.Service.Tenant.XRPATenantOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

@Service
public class XRPASuperAdminService {
    @Autowired
    TenantRepository tenantRepository;
    @Autowired
    XRPATenantOrderService tenantOrderService;

    public Map<String, Object> fetch(int page, int pageSize){
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        PageRequest pr =  PageRequest.of(page,pageSize, sort);
        Page<XRPATenant> pageData = tenantRepository.findAll(pr);
        Map<String, Object> ret = new LinkedHashMap<>();
        ret.put("totalPages", pageData.getTotalPages());
        ret.put("totalCount", pageData.getTotalElements());
        ret.put("page", page);
        ret.put("pageSize", pageSize);
        ret.put("list", pageData.getContent());
        return ret;
    }

    public XRPATenant fetchSingle(int id){
        Optional<XRPATenant> ret = tenantRepository.findById(id);
        if(ret.isEmpty()){
            return null;
        }else{
            return ret.get();
        }
    }

    public Map<String, Object> search(String ttc, String admin, int page, int pageSize){
        if(ttc.length()==0 && admin.length() == 0){
            return this.fetch(0, 10);
        }
        if(ttc.length() == 0){
            ttc = null;
        }
        if(admin.length() == 0){
            admin = null;
        }
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        PageRequest pr =  PageRequest.of(0,10, sort);
        Page<XRPATenant> pageData = tenantRepository.findByTtcOrAdmin(ttc, admin, pr);
        Map<String, Object> ret = new LinkedHashMap<>();
        ret.put("totalPages", pageData.getTotalPages());
        ret.put("totalCount", pageData.getTotalElements());
        ret.put("page", 0);
        ret.put("pageSize", pageSize);
        ret.put("list", pageData.getContent());
        return ret;
    }

    public void delTenant(int id) throws Exception {
        Optional<XRPATenant> ret = tenantRepository.findById(id);
        if(ret.isEmpty()){
            throw new Exception("未找到租户");
        }
        XRPATenant entity = ret.get();
        tenantRepository.delete(entity);
    }


    public void updateTenantInfo(Map<String, Object> reqData) throws Exception {
        int id = (int) reqData.get("id");
        String admin = (String) reqData.get("admin");
        String balance = (String) reqData.get("balance");
        String gift = (String) reqData.get("gift");
        String plan = (String) reqData.get("plan");
        String planStartTime = (String) reqData.get("planStartTime");
        String envLimit = (String) reqData.get("envLimit");
        String userLimit = (String) reqData.get("userLimit");
        String rpaPoints = (String) reqData.get("rpapoints");

        if(id == 0){
            throw new Exception("未找到租户");
        }
        Optional<XRPATenant> ret = tenantRepository.findById(id);
        if(ret.isEmpty()){
            throw new Exception("未找到租户");
        }
        XRPATenant entity = ret.get();
        if(admin!=null) {
            entity.setAdmin(admin);
        }
        if(balance!=null){
            entity.setBalance(Integer.parseInt(balance));
        }
        if(gift!=null){
            entity.setGift(Integer.parseInt(gift));
        }
        if(rpaPoints!=null){
            entity.setRpapoints(Integer.parseInt(gift));
        }
        if(plan!=null){
            entity.setPlan(plan);
        }
        if(planStartTime!=null){
            entity.setPlanStartTime(planStartTime);
        }
        if(envLimit!=null){
            entity.setEnvLimit(Integer.parseInt(envLimit));
        }
        if(userLimit!=null){
            entity.setUserLimit(Integer.parseInt(userLimit));
        }

        tenantRepository.save(entity);
    }

    /**
     * 增加点数, 产生订单信息
     * @param ttc 租户
     * @param points 点数
     */
    public void addRpaPoints(String ttc, Integer points) throws Exception {
        Optional<XRPATenant> ret = tenantRepository.findByTtc(ttc);
        if(ret.isEmpty()){
            throw new Exception("未找到租户");
        }
        XRPATenant entity = ret.get();
        entity.setRpapoints(entity.getRpapoints() + points);
        tenantRepository.save(entity);

        tenantOrderService.createOrder(ttc, "充值", "+"+String.valueOf(points)+"点", TenantOrderEnum.addRPAPointsByAdmin.getMessage());
    }

    /**
     * 增加余额, 产生订单信息
     * @param ttc 租户
     * @param money 钱
     */
    public void addBalance(String ttc, Integer money) throws Exception {
        Optional<XRPATenant> ret = tenantRepository.findByTtc(ttc);
        if(ret.isEmpty()){
            throw new Exception("未找到租户");
        }
        XRPATenant entity = ret.get();
        entity.setBalance(entity.getBalance() + money);
        tenantRepository.save(entity);

        tenantOrderService.createOrder(ttc, "充值", "+"+String.valueOf(money), TenantOrderEnum.AddChargeByAdmin.getMessage());
    }

    /**
     * 增加赠送余额
     * @param ttc 租户
     * @param money 钱
     */
    public void addGift(String ttc, Integer money) throws Exception {
        Optional<XRPATenant> ret = tenantRepository.findByTtc(ttc);
        if(ret.isEmpty()){
            throw new Exception("未找到租户");
        }
        XRPATenant entity = ret.get();
        entity.setGift(entity.getGift() + money);
        tenantRepository.save(entity);

        tenantOrderService.createOrder(ttc, "充值", "+"+String.valueOf(money), TenantOrderEnum.AddGiftByAdmin.getMessage());
    }
}
